/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.interfaces;

import goodenglish.application.LexiconEditService;
import goodenglish.application.impl.LexiconEditServiceImpl;
import goodenglish.domain.model.lexicon.Lexicon;
import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.service.Translator;
import goodenglish.infrastructure.Config;
import goodenglish.infrastructure.EnTTS;
import goodenglish.interfaces.component.MyExplainPane;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.web.WebView;

/**
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class NewWordSetPane extends VBox {

	private LexiconChoiceBox lexiconCbx;
	private FlowPane buttomFlowPane;

	private List<Button> buttons = new ArrayList<Button>();
	private String explaineSparater = "~";
	private String rememberStyle = "-fx-base: green;";
	private String newStyle = "-fx-base: white;";
	private String notSaveedStyle = "-fx-base: red;";
	private String saveedStyle = "-fx-base: lightgray;";
	private MyExplainPane explainPane1;
	private MyExplainPane explainPane2;
	private Button btnRememberSave;
	// 显示提示信息
	private Label reulstLabel;
	private Font font = new Font(20);
	private WebView browser = new WebView();
	private Set<String> rememberSpells = new HashSet<String>();
	private Button analyseBtn;

	public NewWordSetPane() {
		BorderPane.setAlignment(this, Pos.TOP_LEFT);
		this.getChildren().add(getOperateBtnsLine());
		this.getChildren().add(getWordInfoLine());
		this.getChildren().add(addWordBtnsLine());

	}

	private HBox getWordInfoLine() {
		int[] widths = { 40, 25, 25 };
		int height = SH.getH(30);
		HBox hbox = new HBox();
		/**
		 * 添加下方的句子
		 */
		ScrollPane sentencePane = new ScrollPane();
		sentencePane.setMinWidth(SH.getW(widths[0]));
		sentencePane.setMaxWidth(SH.getW(widths[0]));
		sentencePane.setStyle("-fx-border-color: black;-fx-border-width: 2;-fx-alignment:top-left;");
		sentencePane.setContent(browser);

		hbox.getChildren().add(sentencePane);
		/**
		 * 添加解释1
		 */
		explainPane1 = new MyExplainPane("网络微软解释", "ms", SH.getW(widths[1]), height);

		hbox.getChildren().add(explainPane1);
		/**
		 * 添加解释2
		 */
		explainPane2 = new MyExplainPane("本机解释", "local", SH.getW(widths[2]), height);
		hbox.getChildren().add(explainPane2);

		return hbox;
	}

	private ScrollPane addWordBtnsLine() {
		/**
		 * 添加选择认识FlowPanel
		 */
		ScrollPane scrollPane = new ScrollPane();
		buttomFlowPane = new FlowPane(20, 20);
		buttomFlowPane.setMinWidth(Config.SCREEN_WIDTH - 50);
		scrollPane.setMinWidth(Config.SCREEN_WIDTH - 50);
		scrollPane.setMinHeight(300);
		scrollPane.setContent(buttomFlowPane);
		return scrollPane;
	}

	private Pane getOperateBtnsLine() {
		/**
		 * 添加顶部控件组
		 */
		// 控件缝隙
		FlowPane topFlowPane = new FlowPane(10, 10);
		topFlowPane.setMinWidth(Config.SCREEN_WIDTH);
		topFlowPane.setLayoutY(Config.MENU_HEIGHT);

		// 添加文件名显示框
		lexiconCbx = new LexiconChoiceBox();
		topFlowPane.getChildren().add(lexiconCbx);

		/**
		 * 添加字幕分析按钮
		 */
		addAnalyseSubtBtn(topFlowPane);

		/**
		 * 添加保存认识单词按钮
		 */
		btnRememberSave = new Button("保存");
		btnRememberSave.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				if (null != lexicon) {
					lexiconEditService.changeNewWords(lexicon, rememberSpells);
					btnRememberSave.setStyle(saveedStyle);
					analyseBtn.fire();
				}

			}

		});
		topFlowPane.getChildren().add(btnRememberSave);

		/**
		 * 导入信息提示框
		 */
		reulstLabel = new Label();
		reulstLabel.setTextFill(Color.RED);
		topFlowPane.getChildren().add(reulstLabel);
		return topFlowPane;
	}

	/**
	 * 添加分析字幕按钮
	 */
	private void addAnalyseSubtBtn(FlowPane topFlowPane) {

		analyseBtn = new Button("提取生词");
		analyseBtn.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent event) {
				// 判断是否已经导入
				lexicon = lexiconCbx.getSelectLexicon();
				// 句子
				WebVieweHelper.setHtmlText(browser, "");
				// 解释
				explainPane1.setText("");
				explainPane2.setText("");
				// 显示分析结果

				// 删除上一次的按钮
				if (buttons.size() > 0) {
					buttomFlowPane.getChildren().clear();
					buttons = new ArrayList<Button>();
				}
				for (final NewWord newWord : lexicon.getNewWords()) {
					final String spell = newWord.getSpell();
					final Button button = new Button(spell);

					button.setFont(font);
					button.setStyle(newStyle);
					button.setTooltip(new Tooltip(""));
					buttons.add(button);
					buttomFlowPane.getChildren().add(button);
					button.setOnAction(new EventHandler<ActionEvent>() {

						public void handle(ActionEvent event) {
							// 句子
							WebVieweHelper.setHtmlText(browser, newWord.getExampleSentStr());
							// 本地 解释
							explainPane1.refreshSpell(spell);
							explainPane2.refreshSpell(spell);
							// 放入剪切板
							Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
							Transferable tText = new StringSelection(spell);
							clipBoard.setContents(tText, null);
							// 朗读单词
							EnTTS.speak(spell);
							// 第一次点点击，设置词义
							if (!button.getText().contains(explaineSparater)) {
								String explain = translator.englishToChiniseMs(spell);
								button.setText(spell + explaineSparater + explain);

							}

							if (button.getStyle().equalsIgnoreCase(rememberStyle)) {
								// 不认识单词
								button.setStyle(newStyle);
								rememberSpells.remove(spell);
							} else {
								// 认识单词
								button.setStyle(rememberStyle);
								rememberSpells.add(spell);

							}
							// 更新保存按钮
							btnRememberSave.setStyle(notSaveedStyle);
						}
					});

				}

			}
		});
		topFlowPane.getChildren().add(analyseBtn);
	}

	private Lexicon lexicon;
	private LexiconEditService lexiconEditService = BeanFactory.getSingleInstance(LexiconEditServiceImpl.class);
	private Translator translator = BeanFactory.getSingleInstance(Translator.class);

}
